package com.zlx.filter;

import com.zlx.entities.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebFilter("/*")
public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取cookie
        Cookie[] cookies = request.getCookies();
        List<String> list = new ArrayList<>();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                list.add(cookie.getName());
            }
        }
        //获取session域里面的User对象
        User user1 = (User) request.getSession().getAttribute("user");
        if (user1!=null&&!list.contains("username")&&!list.contains("password")){
            Cookie username = new Cookie("username", user1.getName());
            Cookie upassword = new Cookie("password", user1.getPassword());
            //设置cookie的时间
            username.setMaxAge(60 * 60 * 24 * 3);
            upassword.setMaxAge(60 * 60 * 24 * 3);
            response.addCookie(username);
            response.addCookie(upassword);
        }
        String uri = request.getRequestURI();
        if (uri.endsWith("index.jsp") || uri.endsWith("/")) {
            //cookie和密码都存在即可自动登录
            if (list.contains("username") && list.contains("password")) {
                //把用户名和密码使用与转发
                request.getRequestDispatcher("/user/login").forward(request, response);
//                return;
            } else {
                //放行
                filterChain.doFilter(request, response);
            }
            //直接放行
        } else if (uri.endsWith("login")) {//拦截登录
            //直接放行
            filterChain.doFilter(request, response);
        } else {
            HttpSession session = request.getSession();
            //拦截其他页面
            Object user = session.getAttribute("user");
            if (user == null) {
                //如果cookie拥有用户名或密码即可登陆
                if (list.contains("username") && list.contains("password")) {
                    //放行
                    filterChain.doFilter(request, response);
                }else {
                    //用户没登陆
                    request.setAttribute("error", "非法登陆，请登陆后在操作！！！");
                    request.getRequestDispatcher("/index.jsp").forward(request, response);
                    /*response.sendRedirect("/index.jsp");*/
                }
            } else
                filterChain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
